// import axios from "axios";

let cart = {
    state: {
        // cartlist: [{
        //     "oid": "1",
        //     "gid": "6",
        //     "gimgurl": "https://mybanana.com.cn/images/img_14429175059871.jpg",
        //     "gprice": "￥1109",
        //     "qty": "1",
        //     "uid": "1",
        //     "issel": true
        // }, {
        //     "oid": "2",
        //     "gid": "2",
        //     "gimgurl": "https://mybanana.com.cn/images/img_14684905808877.jpg",
        //     "gprice": "￥368",
        //     "qty": "1",
        //     "uid": "1",
        //     "issel": true
        // }, {
        //     "oid": "3",
        //     "gid": "8",
        //     "gimgurl": "https://mybanana.com.cn/images/img_14410065555509.jpg",
        //     "gprice": "￥369",
        //     "qty": "1",
        //     "uid": "2",
        //     "issel": true
        // }]
        cartlist: []
    },
    getters: {
        // 总价
        allprice(state) {
            let selorder = state.cartlist.filter(item => item.issel == true);
            return selorder.reduce((prev, item) => prev + item.gprice.slice(1) * item.qty, 0);
        },
        alllength(state) {
            let selorder = state.cartlist.filter(item => item.issel == true);
            return selorder.length;
        },
        cartlistlength(state) {
            return state.cartlist.length;
        }
    },
    mutations: {
        // 删除商品
        removeItem(state, oid) {
            state.cartlist = state.cartlist.filter(item => item.oid != oid)
        },
        // 添加到购物车
        addToCart(state, data) {
            state.cartlist.unshift(data);
        },
        // 添加数量
        addqty(state, oid) {
            let changorder = state.cartlist.filter(item => item.oid == oid)[0];
            changorder.qty = changorder.qty * 1 + 1;
        },
        // 减少数量
        decqty(state, oid) {
            let changorder = state.cartlist.filter(item => item.oid == oid)[0];
            changorder.qty = changorder.qty - 1;
        }
    },
    actions: {

    }
}

export default cart;